<template>
	<view class="container">
		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
			<!-- 背景色区域 -->
			<view class="titleNview-background" style="background:#E37A64"></view>
			<swiper class="carousel" circular @change="swiperChange">
				<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item" @click="navSwiper(item)">
					<image :src="item.url" />
				</swiper-item>
			</swiper>
			<!-- 自定义swiper指示器 -->
			<view class="swiper-dots" v-if="carouselList.length>0">
				<text class="num">{{swiperCurrent+1}}</text>
				<text class="sign">/</text>
				<text class="num">{{swiperLength}}</text>
			</view>
		</view>
		<view class="box-wrapper">
			<view class="box">
				<view class="box-item" @click="scan">
					<view class="box-img">
						<text class="yticon icon-fahuo"></text>
					</view>
					<view class="box-desc">
						提货
					</view>
				</view>
			</view>
			<view class="box">
				<view class="box-item" @click="navTo('/pages/withdraw/list')">
					<view class="box-img">
						<text class="yticon icon-tixian"></text>
					</view>
					<view class="box-desc">
						提现
					</view>
				</view>
			</view>
			<view class="box">
				<view class="box-item" @click="navTo('/pages/aftersale/list')">
					<view class="box-img">
						<text class="yticon icon-shouhou"></text>
					</view>
					<view class="box-desc">
						售后
					</view>
				</view>
			</view>
			<view class="box">
				<view class="box-item" @click="navTo('/pages/data/data')">
					<view class="box-img">
						<text class="yticon icon-shuju"></text>
					</view>
					<view class="box-desc">
						数据
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniCountdown from "@/components/uni-countdown/uni-countdown.vue"
	import uniNoticeBar from '@/components/uni-notice-bar/uni-notice-bar.vue'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	// #ifdef H5 || APP-PLUS
	const wx = require('weixin-js-sdk');
	// #endif

	export default {
		components: {
			uniCountdown,
			uniNoticeBar
		},
		data() {
			return {
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [],
				cates: [],
				secKills: [],
				secKillCountDown: {},
				groupBuys: [],
				productGroups: [],
				ads: [],
				notice: [],
				announcement: [],
				articleList: [], //图文, 音视频列表
				secKillFlag: 'END' //秒杀结束标志符(END表示已结束)
			};
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'footPrint', 'applicationConfig'])
		},
		onLoad() {
			if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') {
				window.entryUrl = location.href.split('#')[0]
			}
			this.loadData();
		},
		methods: {

			async loadData() {
				this.inquirySwiper();
			},
			//轮播图
			inquirySwiper() {
				this.$api.request.slider({}, res => {
					if (res.body.status.statusCode === '0') {
						this.carouselList = res.body.data.swipers;
						this.swiperLength = this.carouselList.length;
						// this.titleNViewBackground = this.carouselList[0].background;
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			//轮播图链接
			navSwiper(item) {
				debugger
				this.$api.util.navSwiper(item);
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			scan() {
				// #ifdef APP-NVUE || H5
				this.scanH5();
				// #endif

				// #ifdef MP
				this.scanWx();
				// #endif
			},
			scanWx() {
				if(!this.hasLogin){
					uni.navigateTo({
						url: '/pages/public/login'
					})
					return;
				}
				uni.scanCode({
				    success: function (res) {
						var userUuid = res.result;
						console.log('userUuid: '+userUuid);
						uni.navigateTo({
							url: '/pages/order/order?state=3&userId=' + userUuid
						})
				    }
				});

			},
			scanH5() {
				if(!this.hasLogin){
					uni.navigateTo({
						url: '/pages/public/login'
					})
					return;
				}
				var that = this;
				//判断是否时安卓系统获取路径
				let signLink = /(Android)/i.test(navigator.userAgent) ? location.href : window.entryUrl;
				//刷新后路径变化使用当前路径
				if (encodeURIComponent(signLink) == 'undefined') signLink = location.href
				let suplist = {
					url: signLink
				};
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				this.$api.request.getSignature({
					url: signLink
				}, res => {

					if (res.body.status.statusCode === '0') {
						let wxjssdk = res.body.data;
						console.log("wx.config")
						wx.config({
							debug: false,
							appId: wxjssdk.appId,
							timestamp: wxjssdk.timestamp,
							nonceStr: wxjssdk.nonceStr,
							signature: wxjssdk.signature,
							jsApiList: ['scanQRCode']
						})
						wx.ready(function() {
					    console.log("wx.ready")
              setTimeout(() => {
                      wx.scanQRCode({
                        needResult: 1,
                        scanType: ["qrCode", "barCode"],
                        success: function(res) {
                        console.log("wx.scanQRCode")
                        console.log(res)
                          uni.hideLoading();
                          var userUuid = res.resultStr;
                          uni.navigateTo({
                            url: '/pages/order/order?state=3&userId=' + userUuid
                          })
                        }
                      });
                }, 500)
              })
            wx.error(function(res){
              // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
              console.log(res);
            });

            } else {
              console.log(res.body.status.errorDesc);
            }
          });
			},
		},

	}
</script>

<style lang="scss">
	/* #ifdef MP */
	.mp-search-box {
		position: absolute;
		left: 0;
		top: 30upx;
		z-index: 9999;
		width: 100%;
		padding: 0 80upx;

		.ser-input {
			flex: 1;
			height: 56upx;
			line-height: 56upx;
			text-align: center;
			font-size: 28upx;
			color: $font-color-base;
			border-radius: 20px;
			background: rgba(255, 255, 255, .6);
		}
	}

	page {
		.cate-section {
			position: relative;
			z-index: 5;
			border-radius: 16upx 16upx 0 0;
			margin-top: -20upx;
		}

		.carousel-section {
			padding: 0;

			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}

			.carousel {
				.carousel-item {
					padding: 0;
				}
			}

			.swiper-dots {
				left: 45upx;
				bottom: 40upx;
			}
		}
	}

	/* #endif */


	page {
		background: #f5f5f5;
	}

	.m-t {
		margin-top: 16upx;
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;

		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 350upx;
			transition: .4s;
		}
	}

	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}

	.box-wrapper {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		margin-top: 40upx;

		.box {
			width: 50%;
			text-align: center;

			.box-item {
				border-radius: 20upx;
				background-color: #fff;
				margin: 20upx 40upx;
				padding: 40upx 0;
			}

			.yticon {
				font-size: 120upx;
				color: #E07472;
			}

			.box-desc {
				color: $font-color-base;
				font-size: $font-base;
				margin-top: 20upx;
			}
		}
	}
</style>
